<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { addDept } from '@/api/syssetting';
  import { FormInstance, Message } from '@arco-design/web-vue';
  const formRef = ref<FormInstance | null>(null);
  const props = defineProps({
    visible: Boolean,
    isEdit: Boolean,
    currentKey: String | Number,
    editParam: Number,
    formType: Array,
    currentorg: String | Number,
  });
  const emit = defineEmits(['update:visible', 'refresh']);
  const close = () => {
    emit('update:visible', false);
  };

  const handleSubmit = async () => {
    try {
      await formRef.value?.checkForm();
      addDept({
        ...formRef.value?.formModel,
        dept_org_id: props.currentorg,
      })
        .then((res) => {
          Message.success(res.msg);
          emit('refresh');
          close();
        })
        .catch((err) => {});
    } catch (error) {
      // 分类处理错误
      Message.error('请正确填写表单！');
    }
  };

  // 动态生成formData
  const initialFormData = ref({
    ...props.formType.reduce((acc, item) => {
      acc[item.name] = null;
      return acc;
    }, {} as Record<string, any>),
  });

  // 最终使用的formData（保持响应式）
  const formData = ref({ ...initialFormData.value });
  formData.value.dept_parent_id = props.currentKey ? props.currentKey : null;
</script>
<script lang="ts">
  export default {
    name: 'company',
  };
</script>

<template>
  <a-modal
    title="添加部门"
    width="600px"
    :visible="visible"
    @ok="handleSubmit"
    title-align="start"
    @cancel="close"
  >
    <pro-form
      ref="formRef"
      hiddenBtn
      :form-json="formType.filter((item:any) => item.name !== 'dept_id')"
      :form-data="formData"
    ></pro-form>
  </a-modal>
</template>

<style scoped lang="less"></style>
